package com.quanquan.store.controller;

import DTO.Commercial.CommercialInfoDTO;
import DTO.PageDTO;
import com.quanquan.store.dto.CommercialDTO;
import com.quanquan.store.dto.IdDTO;
import com.quanquan.store.entity.Commercial;
import com.quanquan.store.request.CommercialRequest;
import com.quanquan.store.request.PageRequest;
import com.quanquan.store.service.CommercialService;
import enums.ResCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import request.PageRequestBody;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * @ClassName:commercial
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/3/12 11:30
 * @Version 1.0
 */
@RestController
@RequestMapping("/commercial")
public class CommercialController {

    private final CommercialService commercialService;

    @Autowired
    public CommercialController(CommercialService commercialService) {
        this.commercialService = commercialService;
    }

    /**
     * 添加业态
     *
     * @param request
     * @return
     */
    @PostMapping("/add_commercial")
    public ResultBody addCommercial(@RequestBody CommercialRequest request) {
        if (StringUtils.isEmpty(request.getName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "业态名不可为空");
        }
        CommercialDTO commercialDTO = commercialService.saveCommercial(request);
        if (commercialDTO == null) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "新增业态失败");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", commercialDTO);
    }

    /**
     * 查找有效的业态列表
     *
     * @param request
     * @return
     */
    @PostMapping("query_list")
    public ResultBody queryList(@RequestBody PageRequest request) {
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<CommercialDTO> commercialDTOList = commercialService.list(page, size);
        if (commercialDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "暂无有效业态返回");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", commercialDTOList);
    }

    @PostMapping(value = "/query_management")
    @ResponseBody
    public ResultBody queryAllCommercialForManagement(@RequestBody PageRequestBody requestBody) {

        PageDTO<Commercial> floorPageDTO = null;
        floorPageDTO = commercialService.allCommercialForManagement(requestBody);


        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), ResCodeEnum.SUCCESS.getMsg(), floorPageDTO);

    }

    /**
     * 删除id对应业态信息
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_commercial")
    public ResultBody removeCommercial(@RequestBody CommercialRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要删除的业态id");
        }
        Integer mark = commercialService.removeStore(request.getId());
        if (mark == 1) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "删除业态成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "操作失败");
        }
    }

    /**
     * 修改业态信息
     *
     * @param request
     * @return
     */
    @PostMapping("update_commercial")
    @ResponseBody
    public ResultBody updateCommercial(@RequestBody CommercialRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = commercialService.updateCommercial(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }

    /**
     * 根据业态id查找业态
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    @ResponseBody
    public ResultBody queryById(@RequestBody CommercialRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的业态id为空");
        }
        CommercialDTO commercialDTO = commercialService.query(request);
        if (commercialDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此楼层信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", commercialDTO);
    }

    /**
     * 查找有效的业态列表
     *
     * @return
     */
    @PostMapping("com_list")
    public ResultBody comList() {
        List<CommercialDTO> commercialDTOList = commercialService.list();
        if (commercialDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无有效业态返回");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", commercialDTOList);
    }


    @PostMapping("/get_commercial_info")
    @ResponseBody
    public CommercialInfoDTO getCommercialInfo(@RequestBody IdDTO idDTO){
        String commercialId = idDTO.getCommercialId();
        return commercialService.findCommercialInfo(commercialId);
    }

}
